package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import school.domain.News;
import school.mapper.NewsMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;

@RequestMapping("admin/news")
@Controller
public class AdminNewsController extends BaseController{

    @Autowired
    private NewsMapper newsMapper;

    /**
     * 资讯列表
     * @param news
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(News news, @RequestParam(defaultValue = "1") Integer pageNum, Model model){
        PageHelper.startPage(pageNum, 10);
        PageInfo<News> pageInfo = new PageInfo<>(newsMapper.selectList(news), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "admin/news/list";
    }

    /**
     * 删除资讯
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id){
        newsMapper.delById(id);
        return "redirect:/admin/news/list.do";
    }
}
